package cn.myframe.service.sys.impl;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.StringUtils;

import cn.myframe.dao.sys.SysGeneratorDao;
import cn.myframe.entity.sys.ColumnEntity;
import cn.myframe.entity.sys.TableEntity;
import cn.myframe.service.sys.SysGeneratorService;
import cn.myframe.utils.GenUtils;
import cn.myframe.utils.core.PageUtils;

@Service("sysGeneratorService")
public class SysGeneratorServiceImpl implements SysGeneratorService {
	@Autowired
	private SysGeneratorDao sysGeneratorDao;

	@Override
	public List<Map<String, Object>> queryList(Map<String, Object> map) {
		Page<Object> page = PageUtils.getPage(map);
		if(page != null){
			return sysGeneratorDao.queryList(page,map);
		}else{
			return sysGeneratorDao.queryList(map);
		}
		
		//return sysGeneratorDao.queryList(map);
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		return sysGeneratorDao.queryTotal(map);
	}

	@Override
	public Map<String, String> queryTable(String tableName) {
		return sysGeneratorDao.queryTable(tableName);
	}

	@Override
	public List<Map<String, String>> queryColumns(String tableName) {
		return sysGeneratorDao.queryColumns(tableName);
	}

	@Override
	public byte[] generatorCode(String[] tableNames,String dbType) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		
		for(String tableName : tableNames){
			//查询表信息
			Map<String, String> table = queryTable(tableName);
			//查询列信息
			List<Map<String, String>> columns = queryColumns(tableName);
			//生成代码
			GenUtils.generatorCode(table, columns, zip,dbType,1);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}
	
	/**
	 * 刷新列信息
	 * @return
	 */
	public void  refreshColumns(List<Map<String, String>>  columns, Map<String, String> tableMap ){
		for(Map<String, String> column : columns){
			if(column.containsKey("assTable")){
				if(StringUtils.isNotEmpty(column.get("assTable"))){
					String tableName = column.get("assTable");
					//查询表信息
					Map<String, String> table = queryTable(tableName);
					List<Map<String, String>> assColumns = queryColumns(tableName);
					TableEntity tableEntity = GenUtils.buildEntity(table,assColumns);
					column.put("assClassName", tableEntity.getClassName());
					column.put("assclassname", tableEntity.getClassname());
					column.put("assId", tableEntity.getPk().getAttrname());
					column.put("assAttrId", tableEntity.getPk().getAttrName());
					column.put("assTablePrefix", tableEntity.getTablePrefix());
					column.put("assTableName", tableEntity.getTableName());
					column.put("assTableId", tableEntity.getPk().getColumnName());
					for(ColumnEntity columnEntity : tableEntity.getColumns()){
						if(columnEntity.getAttrName().toLowerCase().indexOf("name")>=0){
							column.put("assName", columnEntity.getAttrName());
							column.put("assname", columnEntity.getAttrname());
							column.put("assComments", columnEntity.getComments());
							break;
						}
					}
					
				}
				if(column.containsKey("midAssTable")){
					if(StringUtils.isNotEmpty(column.get("midAssTable"))){
						String tableName = column.get("midAssTable");
						//查询表信息
						Map<String, String> table = queryTable(tableName);
						List<Map<String, String>> assColumns = queryColumns(tableName);
						TableEntity tableEntity = GenUtils.buildEntity(table,assColumns);
						column.put("midClassName", tableEntity.getClassName());
						column.put("midclassname", tableEntity.getClassname());
						column.put("midId", tableEntity.getPk().getAttrname());
						column.put("midTablePrefix", tableEntity.getTablePrefix());
						tableMap.put("hasMidTable", "1");
					}
					
				}
			}
			
		}
	}
	
	
	@Override
	public byte[] generatorCode(String[] tableNames,String dbType,List<Map<String, String>> columns ) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		
		for(String tableName : tableNames){
			//查询表信息
			Map<String, String> table = queryTable(tableName);
			//查询列信息
			//List<Map<String, String>> columns = queryColumns(tableName);
			//刷新列信息
			refreshColumns(columns, table);
			//生成代码
			GenUtils.generatorCode(table, columns, zip,dbType,1);
		}
		//中间表文件生成
		for(Map<String, String> column : columns){
			if(column.containsKey("midAssTable")){
				String tableName = column.get("midAssTable");
				if(StringUtils.isNotEmpty(tableName)){
					//查询表信息
					Map<String, String> table = queryTable(tableName);
					List<Map<String, String>> midColumns = queryColumns(tableName);
					GenUtils.generatorCode(table, midColumns, zip,dbType,0);
				}
			}
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

}
